package com.chinasoftware.civilaviation.controller;

import com.chinasoftware.civilaviation.entity.Admin;
import com.chinasoftware.civilaviation.mapper.AdminMapper;
import com.chinasoftware.civilaviation.service.AdminService;
import com.chinasoftware.civilaviation.service.FlightInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping(value="/admin")
public class LoginController {

	@Autowired
	private AdminService adminService;

	@Autowired
	private AdminMapper adminMapper;

	@Autowired
	private FlightInfoService flightInfoService;

	private final static String ADMIN_ID_GIST = "studentId";

	@GetMapping(value = {"/", "/login"})
	public String login() {
		return "login";
	}

	@GetMapping(value = {"/", "/statistical"})
	public String statistical(@AuthenticationPrincipal User user, HttpSession session) {
		Admin admin = adminMapper.selectOne(new Admin().setAname(user.getUsername()));
		session.setAttribute(ADMIN_ID_GIST, admin.getAname());
		System.out.println("here save");
		return "statistical";
	}

	@GetMapping(value = {"/", "/flightinfos"})
	public String flightInfos() {
		return "flightinfos";
	}

	@GetMapping(value = {"/", "/predict"})
	public String predict() {
		return "predict";
	}

	@GetMapping(value = {"/", "/showmap"})
	public String showMap() {
		return "showmap";
	}

//	@RequestMapping(value = "/login", method = POST)
//	public String login(String aname, String password, HttpServletRequest request, Model model) {
//		Admin admin = adminService.findByAnameAndPassword(aname, password);
//        System.out.println(admin);
//		if(admin!=null) {
//			request.getSession().setAttribute("admin", admin);
//			System.out.println("hello");
////			model.addAttribute("list", flightInfoService.findAll());
////			return "flightinfo";
////            model.addAttribute("list", goodsService.findAll());
////            return "goods";
//            return "statistical";
//		}else {
//			request.setAttribute("msg", "��������û����������������������룡");
//			return "login";
//
//		}
//	}

	@ResponseBody
	@RequestMapping(value = "/register", method = POST)
	public Map<String, Object> register(String account, String password, Model model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			adminMapper.insertSelective(new Admin().setAname(account).setPassword(password));
			resultMap.put("State", "true");
		}catch (Exception e) {
			resultMap.put("State", "false");
			resultMap.put("message", "用户名重复");
		}
		return resultMap;
	}

	@RequestMapping("logout")
	private String logout(HttpServletRequest request)  throws ServletException, IOException {
		request.getSession().invalidate();
		request.setAttribute("msg", "���Ѱ�ȫ�˳���ϵͳ����ӭ�ٴη��ʣ�");
		return "login";
	}
}
